$(function() {
	// 页面控件
	var $dataTable = $('#dataTable'); // 表格
	var $dataTableToolbar = $('#dataTableToolbar'); // 表格：工具栏
	var $btnByMiner = $('#btnByMiner');
	var $btnByShelf = $('#btnByShelf');
	var $cachedTime = $('#cachedTime');
	
	initData();
	buildMinerTable();
	
	function initData(){
		refreshCachedTime();
		$.ajax({
			url: "states/miners",
			type: "GET",
			contentType: "application/json; charset=utf-8",
			success: function(result){
				if(result.success){
					var mine = result.data;
					$dataTable.bootstrapTable('load', mine);
				}
			}
		});
	}
	
	function refreshCachedTime() {
		$.ajax({
			url: 'states/time',
			type: 'GET',
			dataType: 'json',
			contentType: "application/json; charset=utf-8",
			success: function(result) {
				if (result.success) {
					$cachedTime.text(result.data);
				}
			}
		});
	}
	
	function buildMinerTable() {
		$dataTable.bootstrapTable({
			showRefresh: true,
			columns: [
			    [
					{
						field: 'shelfName',
						title: '所属机架',
						halign: 'center',
						align: 'left',
						valign: 'middle',
						rowspan: 2
					},
					{
						field: 'host',
						title: 'IP地址',
						halign: 'center',
						align: 'left',
						valign: 'middle',
						rowspan: 2
					},
					{
						field: 'pingable',
						title: '网络情况',
						align: 'center',
						valign: 'middle',
						rowspan: 2,
					},
					{
						field: 'mac',
						title: 'MAC地址',
						align: 'center',
						valign: 'middle',
						rowspan: 2,
					},
					{
						field: 'runtime',
						title: '运行时状态',
						align: 'center',
						valign: 'middle',
						rowspan: 2,
						formatter: function(value, row, index) {
							var and = row.statsable & row.poolsable & row.summaryable;
							var or = row.statsable | row.poolsable | row.summaryable;
							if (and) {
								return '<img src="../img/icon/icon-check.png" width="20" height="20" />';
							} else if (or) {
								return '<img src="../img/icon/icon-check.png" width="20" height="20" />';
							} else {
								return '<img src="../img/icon/icon-cross.png" width="20" height="20" />';
							}
						}
					},
					{
						field: 'minerType',
						title: '矿机型号',
						align: 'center',
						valign: 'middle',
						rowspan: 2
					},
					{
						field: 'firmwareVersion',
						title: '固件版本',
						align: 'center',
						valign: 'middle',
						rowspan: 2
					},
					{
						field: 'pools',
						title: '矿池信息',
						align: 'center',
						colspan: 3
					},
					{
						field: 'operate',
						title: '操作',
						align: 'center',
						valign: 'middle',
						rowspan: 2,
					}
			    ],
			    [
			     	{
			     		field: 'pool1',
			     		title: '1',
			     		halign: 'center',
						align: 'left',
			     	},
			     	{
			     		field: 'pool2',
			     		title: '2',
			     		halign: 'center',
						align: 'left',
			     	},
			     	{
			     		field: 'pool3',
			     		title: '3',
			     		halign: 'center',
						align: 'left',
			     	}
			    ]
			]
		});
	}
});